package top.chengdongqing.mishop.common.sender

import org.apache.logging.log4j.LogManager
import org.springframework.beans.factory.annotation.Value
import org.springframework.mail.SimpleMailMessage
import org.springframework.mail.javamail.JavaMailSender
import org.springframework.stereotype.Component

@Component
class EmailSender(
    private val javaMailSender: JavaMailSender,
    @Value("\${spring.mail.username}") private val username: String
) : Sender {
    private val log = LogManager.getLogger(EmailSender::class.java)

    override fun send(recipient: String, subject: String, message: String) {
        val mailMessage = SimpleMailMessage()
        mailMessage.from = username
        mailMessage.setTo(recipient)
        mailMessage.subject = subject
        mailMessage.text = message

        javaMailSender.send(mailMessage)
        log.info("Sending email to {}: {} | {}", recipient, subject, message)
    }
}